package com.sunseagear.wind.modules.sms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sms.entity.SmsSendLog;
import com.sunseagear.wind.modules.sms.entity.query.SmsSendLogQuery;
import com.sunseagear.wind.modules.sms.service.ISmsSendLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.sms.controller
 * @title: 发送日志控制器
 * @description: 发送日志控制器 * @date: 2018-09-14 09:47:53
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("sms/sendlog")
@Log(title = "短信发送日志")
@RequiredArgsConstructor
public class SmsSendLogController extends BaseBeanController {

    private final ISmsSendLogService smsSendLogService;


    /**
     * 短信发送日志
     * @param query 查询条件
     * @return 查询结果
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sms:sendlog:list")
    public RPage<SmsSendLog> list(SmsSendLogQuery query) {
        //加入条件
        QueryWrapper<SmsSendLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(SmsSendLog::getResponseDate)
                .eq(ObjectUtils.isNotEmpty(query.getSendCode()),SmsSendLog::getSendCode, query.getSendCode())
                .eq(ObjectUtils.isNotEmpty(query.getPhone()),SmsSendLog::getPhone, query.getPhone())
                .eq(ObjectUtils.isNotEmpty(query.getStatus()),SmsSendLog::getStatus, query.getStatus());
        // 预处理
        Page<SmsSendLog> pageBean = smsSendLogService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 短信发送日志删除
     * @param idList id列表
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sms:sendlog:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        smsSendLogService.removeBatchByIds(idList);
        return R.success("删除成功");
    }

    /**
     * 重发短信
     * @param idList id列表
     * @return 重发结果
     */
    @PostMapping(value = "retrySend")
    public R<Void> retrySend(@RequestBody List<Long> idList) {
        try {
            smsSendLogService.retrySend(idList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("重发队列添加失败");
        }
        return R.success("重发队列添加成功");
    }
}
